<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @package   App_Backend
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: ConfigurationController.php 20 2010-12-18 09:35:07Z cheng.wei $$
 * @copyright Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Configure controller for backend
 * 
 * @package   App_Backend
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: ConfigurationController.php 20 2010-12-18 09:35:07Z cheng.wei $$
 */
class Default_ConfigurationController extends Evebit_Controller_Action_Backend {
	
	/**
	 * grid
	 */
	public function indexAction() {
		$session = $this->getSession();
		$session->initGrid('configs', array(), array(), array('size'=> 10, 'order'=> 'asc'));
		
		$config = new Default_Model_Entity_Config();
		$collection = $config->getCollection();
		
		$adapter = new Evebit_Model_Paginator_Adapter($collection);
		$paginator = new Zend_Paginator($adapter);
		
		$session->applyCollection($collection, $paginator);
		
		$this->view->collection = $collection;
		
		$this->view->paginator = $paginator;
		$this->view->state = $session->getGridState();
	}
	
   /**
	 * add action of the comment controller
	 * 
	 * add a article
	 * 
	 */
	public function addAction() {
		$this->_forward ( 'edit', null, null, array ('id' => 0 ) );
	}
	
	
	/**
	 * reset grid
	 */
	public function resetAction() {
		$this->getSession()->resetGrid();
		$this->_forward("index", "configuration");
	}
	
	/**
	 * edit an existsing configuration
	 */
	public function editAction() {
		$id = $this->getRequest()->getParam('id');
		$config = new Default_Model_Entity_Config();
		if ($id) {
			$config->load($id);
		}
		$this->view->data = $config;
	}
	
	/**
	 * save configuration
	 */
	public function saveEditAction() {
		$admin = new Default_Model_Entity_Config();
		$data = $this->getRequest()->getPost('data');
		
		if ($data['id']) {
			$admin->load($data['id']);
		}
		
		$admin->addData($data);
		
		try {
			$response = $admin->save();
			Evebit_Event::event('insert_log_data', $this , $response );
		} catch (Exception $e) {
			$this->getSession()->addSuccess($this->__("Save failed"));
			return $this->_forward('edit', 'configuration', null, array('id'=> $data['id']));
		}
		
		$this->getSession()->addSuccess($this->__("Save Success"));
		$this->_redirect("configuration");
	
	}

}

